import React from "react";

interface IconProps {
  src: string;
  className?: string;
  style?: React.CSSProperties;
  onClick?: () => void;
}

const Icon: React.FC<IconProps> = ({ src, className, style = {}, onClick }) => {
  // 处理图片路径
  let imagePath = src;

  // 移除开头的斜杠
  if (src.startsWith("/")) {
    imagePath = src.substring(1);
  }

  return (
    <img
      src={imagePath}
      alt="icon"
      className={className}
      style={style}
      onClick={onClick}
      onError={(e) => {
        console.error(`图片加载失败: ${imagePath}`);
        // 尝试加载降级路径
        const imgElement = e.currentTarget as HTMLImageElement;
        if (imagePath.startsWith("images/")) {
          // 尝试相对路径
          imgElement.src = imagePath.substring(7); // 去掉 "images/" 前缀
        }
      }}
    />
  );
};

export default Icon;
